<template>
  <div class="article-detail-container">
    <!-- 文章标题 -->
    <el-card class="article-card" v-if="currDetailData">
      <h1 class="article-title">{{ currDetailData.title }}</h1>
      <div class="article-meta">
        <!-- <span class="author">作者: {{ currDetailData.author }}</span>? -->
        <span class="date">发布时间: {{ timestampToDayHM(currDetailData.updatedAt) }}</span>
      </div>
      <div class="comments-title">活动时间：<span class="mdata">{{ timestampToDayHM(currDetailData.activityDate) }}</span></div>
      <div class="comments-title">活动类型：<span class="mdata">{{ currDetailData.tags }}</span></div>
      <div class="comments-title">已报名人数：<span class="mdata">{{ currDetailData.favs }}</span></div>
      <div class="article-content">
        <div class="comments-title">活动详情</div>
        <div>
          {{ currDetailData.content }}
        </div>
      </div>

      <!-- 点赞与收藏按钮 -->
    <div class="action-buttons" v-if="currDetailData.status==1">
      <!-- <el-button v-if="currDetailData.isLiked" type="primary" icon="ArrowUpBold" @click="toggleLike">
        {{ currDetailData.likes }}点赞
      </el-button>
      <el-button v-else type="primary" plain icon="ArrowUpBold" @click="toggleLike">
        {{ currDetailData.likes }}点赞
      </el-button> -->
      <el-button v-if="currDetailData.isFavorited" type="success" icon="SuccessFilled" @click="toggleFavorite">
        已报名
      </el-button>
      <el-button v-else type="danger" plain icon="CircleCheck" @click="toggleFavorite">
        我要报名
      </el-button>
    </div>
    <div class="action-buttons" v-if="currDetailData.status==2">
      <el-button type="info" icon="WarningFilled">
        已结束
      </el-button>
    </div>

      <!-- 图片缩略图列表 -->
    <div class="image-thumbnails">
      <!-- <el-image
        v-for="(image, index) in currDetailData.images"
        :key="index"
        :src="image.thumbnail"
        :preview-src-list="currDetailData.images.map(img => img.large)"
        :initial-index="index"
        class="thumbnail"
        fit="cover"
        hide-on-click-modal
      ></el-image> -->

      <el-image
        style="width:100px; height:100px;"
        v-for="(image, index) in currDetailData.picList" 
        :key="index"
        :src='image'
        :zoom-rate="1.2"
        :max-scale="7"
        :min-scale="0.2"
        :preview-src-list="currDetailData.picList"
        show-progress
        fit="cover"
      />
    </div>
    </el-card>

    <!-- 评论列表 -->
    <!-- <el-card class="comments-card">
      <h2 class="comments-title">评论</h2>
      <div class="comment-list">
        <div v-for="comment in commentList" :key="comment.id" class="comment-item">
          <div class="comment-author">
            <el-avatar shape="square" size="medium" :src="comment.user.pic" style="float: left; margin-right: 10px;"></el-avatar>
            {{ comment.user.nickName }}
            <div class="comment-date">{{ timestampToDayHM(comment.updatedAt) }}</div>
          </div>
          <div class="comment-content">{{ comment.content }}</div>
        </div>
      </div>
      <p class="no-data" v-if="commentList.length==0">暂无评论</p>
    </el-card> -->

    <!-- 发表评论 -->
    <!-- <el-card class="add-comment-card">
      <h2 class="add-comment-title">发表评论</h2>
      <el-form label-width="130px" :model="editForm" :rules="rules" ref="formRef">
        <el-input
          v-model="editForm.content"
          :rules="rules"
          type="textarea"
          :rows="4"
          placeholder="请输入你的评论"
        ></el-input>
        <el-button type="primary" class="submit-comment-btn" @click="submitForm">提交评论</el-button>
      </el-form>
    </el-card> -->
  </div>
</template>
  
<script setup lang="ts">
  import { ref, reactive, nextTick, toRef } from "vue";
  import baseService from "@/service/baseService";
  import { ElMessage, ElMessageBox } from "element-plus";
  import { timestampToDayHM, timestampToHM, timestampToDate, formatAmount } from "@/utils/utils"
  import { fileUploader } from '@/utils/upload.js'
  import { useRouter, useRoute } from 'vue-router';

  // 查询参数
  const queryForm = reactive({
    deptName: "",
    pageNo: 1,
    pageSize: 10
  });
  const router = useRouter();
  const route = useRoute();
  // 列表内容数量
  const total = ref(0);
  // 列表是否加载
  const loading = ref(true);
  // 列表返回值
  const list = ref<any[]>([]);

  // 表单实例
  const formRef = ref();
  // 是否打开弹出框
  const open = ref(false);
  const openDetail = ref(false);
  // 弹出框标题
  const title = ref("");
  const commentList = ref<any[]>([]);
  const currDetailData = ref();
  
  // 提交表单数据
  let editForm = toRef(
    reactive({
      newsId: '',
      content: ''
    })
  );

  // 表单验证
  const rules = ref({
    content: [{ required: true, message: "规则名称不能为空", trigger: "blur" }],
  });

  /**
   * 表单重置
   */
   function reset() {
    editForm.value = {
      newsId: '',
      content: ''
    };
    nextTick(() => {
      formRef.value?.resetFields();
    });
  }

  function goBack(){
    router.back()
  }

  const getInfo = () => {
      const pid = route.query.nid
      console.log('从路由获取参数：', route.query)
      baseService.get(`/api/activity/detail/${pid}`).then((res) => {
          console.log('获取详情：',res)
          currDetailData.value = res.data.news;
          commentList.value = res.data.commentList;
          loading.value = false;
      }).catch(() => {
          loading.value = false;
      });
  }

  //点赞
  const toggleLike = () => {
    const pid = route.query.nid
    baseService.get(`/api/activity/like/${pid}`).then((res) => {
      loading.value = false;
      if(currDetailData.value.isLiked==0){
        currDetailData.value.isLiked=1
        currDetailData.value.likes++
        ElMessage.success('点赞成功')
      }else{
        currDetailData.value.isLiked=0
        currDetailData.value.likes--
        ElMessage.warning('已取消点赞')
      }
    }).catch(() => {
        loading.value = false;
    });
  }

  //收藏 
  const toggleFavorite = () => {
    const pid = route.query.nid
    baseService.get(`/api/activity/fav/${pid}`).then((res) => {
      loading.value = false;
      if(currDetailData.value.isFavorited==0){
        currDetailData.value.isFavorited=1
        currDetailData.value.favs++
        ElMessage.success('报名成功')
      }else{
        currDetailData.value.isFavorited=0
        currDetailData.value.favs--
        ElMessage.warning('已取消报名')
      }
    }).catch(() => {
        loading.value = false;
    });
  }

  //发表评论
  function submitForm() {
    formRef.value.validate((valid: boolean) => {
      if (!valid) return;
      editForm.value.newsId = currDetailData.value.id
      baseService.post(`/api/activity/comment`, editForm.value).then((res) => {
        if (res.code === 200) {
          ElMessage.success(res.msg);
          open.value = false;
          editForm.value.content=''
          getInfo();
        } else {
          ElMessage.error(res.msg);
        }
      });
    });
  }

  getInfo();
</script>
  
<style scoped>
  .article-detail-container {
    max-width: 800px;
    margin: 0 auto;
    padding: 20px;
  }
  
  .article-card {
    margin-bottom: 20px;
  }
  
  .article-title {
    font-size: 24px;
    margin-bottom: 10px;
    color: #666;
  }
  
  .article-meta {
    font-size: 14px;
    color: #666;
    margin-bottom: 20px;
  }
  
  .article-content {
    font-size: 16px;
    line-height: 1.6;
    margin: 20px 0;
    white-space:break-spaces;
  }
  
  .image-thumbnails {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
  }
  
  .thumbnail {
    width: 150px;
    height: 150px;
    cursor: pointer;
    border-radius: 4px;
    transition: transform 0.2s;
  }

  
  .comments-card {
    margin-bottom: 20px;
  }
  
  .comments-title {
    font-size: 18px;
    margin-bottom: 15px;
    font-weight: normal;
    color: #666;
  }
  .mdata{
    color:#c35556;
  }
  
  .comment-item {
    margin-bottom: 15px;
    padding-bottom: 15px;
    border-bottom: 1px solid #eee;
  }
  
  .comment-author {
    font-weight: bold;
    margin-bottom: 5px;
    height: 45px;
  }
  
  .comment-content {
    font-size: 14px;
    color: #333;
    margin-bottom: 5px;
  }
  
  .comment-date {
    font-size: 12px;
    color: #999;
    padding-top: 8px;
  }
  
  .add-comment-card {
    margin-bottom: 20px;
  }
  
  .add-comment-title {
    font-size: 20px;
    margin-bottom: 15px;
    color:#555;
    font-weight: normal;
  }
  
  .submit-comment-btn {
    margin-top: 10px;
  }

  .action-buttons {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
  }
  .no-data{
    margin: 10px 0;
    color:#888;
  }
</style>